from peewee import *
from user_srv.settings import settings


class BaseModel(Model):
    class Meta:
        database = settings.DB


class User(BaseModel):
    GENDER_CHOICES = (
        ("female", "女"),
        ("male", "男")
    )
    ROLE_CHOICES = (
        (1, "普通用户"),
        (2, "管理员")
    )
    mobile = CharField(max_length=11, index=True, unique=True, verbose_name="手机号码")
    password = CharField(max_length=100, verbose_name="密码")
    nick_name = CharField(max_length=20, null=True, verbose_name="昵称")
    head_url = CharField(max_length=200, null=True, verbose_name="头像")
    birthday = DateField(null=True, verbose_name="生日")
    address = CharField(null=True, verbose_name="地址", max_length=200)
    desc = TextField(null=True, verbose_name="个人简介")
    gender = CharField(max_length=6, null=True, verbose_name="性别", choices=GENDER_CHOICES)
    role = IntegerField(default=1, choices=ROLE_CHOICES, verbose_name="用户角色")


if __name__ == '__main__':
    settings.DB.create_tables([User])
    from datetime import date
    import time
    users = User.select()
    for user in users:
        if user.birthday:
            print(user.birthday) # 从数据库获取时间类型，是date类型
            u_time = int(time.mktime(user.birthday.timetuple())) # 将date类型转换成int时间戳类型,为了在proto里数据传输
            print(u_time)
            print(date.fromtimestamp(u_time)) # 将Int类型转换成 date类型
